package com.smart.smartdata;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;

import java.util.ArrayList;
import java.util.List;

/**
 * author : admin
 * time   : 2017/11/13
 * desc   : RecyclerView初始化工具类
 */
public class RcvInitUtils {

    // 初始化一般瀑布流RecyclerView
    public static void initStaggeredGridRcv(RecyclerView mRcv, int spanCount, BaseQuickAdapter mAdapter) {

        mRcv.setHasFixedSize(true);
        mRcv.setOverScrollMode(View.OVER_SCROLL_NEVER);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(spanCount, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        layoutManager.setAutoMeasureEnabled(true);
        mRcv.setLayoutManager(layoutManager);
        mRcv.setAdapter(mAdapter);
    }


    // 初始化一般网格RecyclerView
    public static void initGridRcv(Context context, RecyclerView mRcv, int spanCount, BaseQuickAdapter mAdapter) {

        mRcv.setHasFixedSize(true);
        mRcv.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRcv.setLayoutManager(new GridLayoutManager(context, spanCount));
        mRcv.setAdapter(mAdapter);
    }

    // 初始化一般横向RecyclerView
    public static void initHorizontalRcv(Context context, RecyclerView mRcv, RecyclerView.Adapter mAdapter) {
        mRcv.setHasFixedSize(true);
        mRcv.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRcv.setLayoutManager(new LinearLayoutManagerPlus(context, LinearLayoutManager.HORIZONTAL, false));
        mRcv.setAdapter(mAdapter);
    }

    // 初始化一般纵向RecyclerView
    public static void initVerticalRcv(Context context, RecyclerView mRcv, BaseQuickAdapter mAdapter) {

        mRcv.setHasFixedSize(true);
        mRcv.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRcv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        mRcv.setAdapter(mAdapter);
    }

    // 初始化下拉功能纵向RecyclerView
    public static void initVerticalRcv(Context context, TwinklingRefreshLayout mRefreshLayout, RecyclerView mRcv, BaseQuickAdapter mAdapter, RefreshListenerAdapter mRefreshListener) {

        // TwinklingRefreshLayout 初始化
        mRefreshLayout.setEnableLoadmore(true);
        mRefreshLayout.setEnableRefresh(true);
        mRefreshLayout.setHeaderView(new SinaRefreshView(context));
        mRefreshLayout.setOnRefreshListener(mRefreshListener);
        mRefreshLayout.setTargetView(mRcv);
       // mRefreshLayout.setFloatRefresh(true);
        //        mRefreshLayout.setPureScrollModeOn();
        initVerticalRcv(context, mRcv, mAdapter);
    }

    // 初始化下拉功能瀑布流RecyclerView
    public static void initStaggeredGridRcv(Context context, TwinklingRefreshLayout mRefreshLayout, RecyclerView mRcv, int spanCount, BaseQuickAdapter mAdapter, RefreshListenerAdapter mRefreshListener) {

        // TwinklingRefreshLayout 初始化
        mRefreshLayout.setEnableLoadmore(true);
        mRefreshLayout.setEnableRefresh(true);
        mRefreshLayout.setHeaderView(new SinaRefreshView(context));
        mRefreshLayout.setOnRefreshListener(mRefreshListener);
        mRefreshLayout.setFloatRefresh(true);
        initStaggeredGridRcv(mRcv, spanCount, mAdapter);
    }

    // 初始化下拉功能网格RecyclerView
    public static void initGridRcv(Context context, TwinklingRefreshLayout mRefreshLayout, RecyclerView mRcv, int spanCount, BaseQuickAdapter mAdapter, RefreshListenerAdapter mRefreshListener) {
        // TwinklingRefreshLayout 初始化
        mRefreshLayout.setEnableLoadmore(true);
        mRefreshLayout.setEnableRefresh(true);
        mRefreshLayout.setHeaderView(new SinaRefreshView(context));
        mRefreshLayout.setOnRefreshListener(mRefreshListener);
        mRefreshLayout.setFloatRefresh(true);

        initGridRcv(context, mRcv, spanCount, mAdapter);
    }

    // 初始化一般流式布局
    public static void initFlowRcv(RecyclerView mRcv, BaseQuickAdapter mAdapter) {
        mRcv.setHasFixedSize(true);
        mRcv.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRcv.addItemDecoration(new FlowLayoutItemDecoration(ScreenUtil.dip2px(10)));
        mRcv.setLayoutManager(new FlowLayoutManager());
        mRcv.setAdapter(mAdapter);
    }



    // 初始化标签键盘流式布局
    public static void initTagKeyboardFlowRcv(Context context, RecyclerView mRcv, BaseQuickAdapter mAdapter, FlowLayoutManager flowLayoutManager) {
        mRcv.setHasFixedSize(true);
        mRcv.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRcv.addItemDecoration(new FlowLayoutItemDecoration(ScreenUtil.dip2px(10)));
        mRcv.setLayoutManager(flowLayoutManager);
        mRcv.setAdapter(mAdapter);
    }

    // 初始化标签键盘流式布局
    public static void initTagKeyboardFlowRcv(RecyclerView mRcv, BaseQuickAdapter mAdapter, FlowLayoutManager flowLayoutManager) {
        mRcv.setHasFixedSize(true);
        mRcv.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRcv.addItemDecoration(new FlowLayoutItemDecoration(ScreenUtil.dip2px(10)));
        mRcv.setLayoutManager(flowLayoutManager);
        mRcv.setAdapter(mAdapter);
    }

    // 初始化标签键盘流式布局
    public static void initTagKeyboardFlowRcv(RecyclerView mRcv, BaseQuickAdapter mAdapter, FlowLayoutManager flowLayoutManager, float dipValue) {
        mRcv.setHasFixedSize(true);
        mRcv.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRcv.addItemDecoration(new FlowLayoutItemDecoration(ScreenUtil.dip2px(dipValue)));
        mRcv.setLayoutManager(flowLayoutManager);
        mRcv.setAdapter(mAdapter);
    }


    /**
     * 规范处理列表刷新事件
     *
     * @param mRefreshRcv 数据列表容器
     * @param mAdapter    数据列表适配器
     * @param emptyView   空数据页面
     * @param pageSize    每一页加载的数据条数
     * @param been        新数据
     * @param loadMode    操作模式 0 下拉刷新 1 上拉加载
     */
    public static <T extends BaseQuickAdapter, K> void onRefresh(TwinklingRefreshLayout mRefreshRcv, T mAdapter, View emptyView, int pageSize, List<K> been, int loadMode) {
        if (null == been) {
            been = new ArrayList<>();
          //  return;
        }
        //根据数据量开闭上拉功能
        if (been.size() <= pageSize) {
            mRefreshRcv.setEnableLoadmore(false);
        } else {
            mRefreshRcv.setEnableLoadmore(true);
        }

        if (loadMode == 0) {//刷新列表
            mAdapter.setNewData(been);
        } else {//加载更多
            mAdapter.addData(been);
        }

        if (mAdapter.getData().size() == 0 && emptyView != null) {
            mAdapter.setEmptyView(emptyView);
        }
        loadComplete(mRefreshRcv,loadMode);
    }

    /**
     * 规范处理列表刷新事件
     *
     * @param mRefreshRcv 数据列表容器
     * @param mAdapter    数据列表适配器
     * @param emptyView   空数据页面
     * @param total    一共据条数
     * @param been        新数据
     * @param loadMode    操作模式 0 下拉刷新 1 上拉加载
     */
    public static <T extends BaseQuickAdapter, K> void onRefresh2(TwinklingRefreshLayout mRefreshRcv, T mAdapter, View emptyView, int total, List<K> been, int loadMode) {
        if (null == been) {
            been = new ArrayList<>();
            //  return;
        }
        //根据数据量开闭上拉功能


        if (loadMode == 0) {//刷新列表
            mAdapter.setNewData(been);
        } else {//加载更多
            mAdapter.addData(been);
        }

        if (mAdapter.getData().size() == total) {
            mRefreshRcv.setEnableLoadmore(false);
        } else {
            mRefreshRcv.setEnableLoadmore(true);
        }

        if (mAdapter.getData().size() == 0 && emptyView != null) {
            mAdapter.setEmptyView(emptyView);
        }
        loadComplete(mRefreshRcv,loadMode);
    }


    /**
     * 请求结束
     *
     * @param tkRefresh 刷新控件
     * @param loadMode  加载模式  0 下拉刷新 1 上拉加载
     */
    public static void loadComplete(TwinklingRefreshLayout tkRefresh, int loadMode) {
        if (null == tkRefresh) {
            return;
        }
        if (loadMode == 0) {
            tkRefresh.finishRefreshing();
        } else if (loadMode == 1) {
            tkRefresh.finishLoadmore();
        }
    }






    /**
     * 获取RecyclerView滑动距离
     *
     * @return dy
     */
    public static int getScrollYDistance(LinearLayoutManager layoutManager) {
        int position = layoutManager.findFirstVisibleItemPosition();
        View firstVisibleChildView = layoutManager.findViewByPosition(position);
        if (null != firstVisibleChildView) {
            int itemHeight = firstVisibleChildView.getHeight();
            return (position) * itemHeight - firstVisibleChildView.getTop();
        }
        return 0;
    }


    public static View getEmptyView(Activity context, RecyclerView mRcv, String content) {
        if(null == context){
            return null;
        }
        LayoutInflater layoutInflater = context.getLayoutInflater();
        if(null != layoutInflater){
            View emptyView =layoutInflater.inflate(R.layout.layout_emptyview_e, null, false);

            if (!TextUtils.isEmpty(content)) {
                ((TextView) emptyView.findViewById(R.id.tv_empty_content)).setText(content);
            }


            return emptyView;
        }else {
            return null;
        }

    }



}
